﻿Imports System.Data
Imports CrystalDecisions.Shared
Imports System.Data.OleDb
Imports System
Imports System.Globalization

Partial Class Interfaz_ModuloPlanificacion_frmIngresoPlanificacion
    Inherits System.Web.UI.Page

    Dim CL As Logica.ModuloPlanificacion.ControladorLogica
    Dim CLAdmin As Logica.ModuloAdministracion.ControladorLogica
    Dim dtDatosPlanificacion As New DataTable

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        CL = Logica.ModuloPlanificacion.ControladorLogica.getInstance
        CLAdmin = Logica.ModuloAdministracion.ControladorLogica.getInstance

        If CL.visibilidadesFormsInternos("Planificacion", Session("user").ToString) = False Then
            Me.Mensaje("Usted NO cuenta con los permisos necesarios para ingresar a esta sección. Para solucionar este problema favor comunicarse con el administrador de sistemas")
            Response.Redirect("Index.aspx")
        End If

        If Not Page.IsPostBack Then
            Try
                llenarCBot()
                ValidarCampos()
                dtDatosPlanificacion.Columns.Add(New DataColumn("NPLANIFICACION"))
                dtDatosPlanificacion.Columns.Add(New DataColumn("FECHA"))
                Me.llenarGridPlanificacionPrincipal() 
                Me.GridItems.DataBind()
                Me.GridItems.DataSource = Me.dtDatosPlanificacion
            Catch ex As Exception
                Mensaje(ex.Message)
            End Try

        Else
        End If
        Me.GridItems.DataSource = dtDatosPlanificacion
    End Sub

#Region "Utilidades"

    Public Sub Mensaje(ByVal msg As String)
        Dim alertScript As String = "<script language=JavaScript runat=server>"
        alertScript &= "alert('" & msg & "');"
        alertScript &= "</script>"
        ClientScript.RegisterClientScriptBlock(Page.GetType(), "clientScrip", alertScript)
    End Sub

    Public Sub ValidarCampos()
        Me.TxtDensidad.Attributes.Add("onkeypress", "javascript:return ValidNum(event);")
    End Sub

    Public Sub llenarCBot()

        Dim ot As ArrayList = CL.ObtenerOT("", 1)

        If ot.Count > 0 Then
            For i As Integer = 0 To ot.Count - 1
                Dim item As New ListItem
                Dim presupuesto As ArrayList = Me.CLAdmin.ObtenerPresupuestoCriterio(DirectCast(ot.Item(i), OrdenTrabajo).FkPresupuesto)
                item.Value = DirectCast(ot.Item(i), OrdenTrabajo).Id
                item.Text = DirectCast(ot.Item(i), OrdenTrabajo).Id & " - " & (DirectCast(presupuesto.Item(0), Presupuesto).atencion).ToString
                Me.DdlOT.Items.Add(item)
            Next i

        End If
    End Sub

#End Region

    Public Sub llenarGridPlanificacionPrincipal()

        Dim data As ArrayList = CL.ObtenerPlanificacionPrincipal(Me.DdlOT.Text, 3)
        If data.Count > 0 Then
            For i As Integer = 0 To data.Count - 1
                Dim drDatos As DataRow = dtDatosPlanificacion.NewRow
                Dim plan As Planificacion = DirectCast(data.Item(i), Planificacion)

                drDatos.Item("NPLANIFICACION") = plan.Id
                drDatos.Item("FECHA") = plan.fecha

                dtDatosPlanificacion.Rows.Add(drDatos)

            Next
            Me.GridItems.DataSource = dtDatosPlanificacion
            Me.GridItems.DataBind()
            Me.LblMsje.Text = ("Los Datos Han Sido Ingresados Exitosamente")
            Me.LblMsje.ForeColor = Drawing.Color.Blue
            Me.LblMsje.Font.Bold = False
        Else
            Me.LblMsje.Text = ("La OT Seleccionada No Cuenta Con Planificaciones")
            Me.LblMsje.ForeColor = Drawing.Color.Red
            Me.LblMsje.Font.Bold = False
            Me.dtDatosPlanificacion.Clear()
            Me.GridItems.DataSource = Me.dtDatosPlanificacion
            Me.GridItems.DataBind()
        End If
    End Sub

    Protected Sub GridItems_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridItems.RowCommand
        Try
            Dim accion As String = e.CommandName.ToString
            Dim url As String = ""
            Dim index As String = CInt(e.CommandArgument)
            Dim subProy As String = Me.GridItems.Rows(index).Cells(0).Text

            If accion = "EditarPlanificacion" Then
                url = """PopUpPlanificacion1.aspx?NplanPpal=" & subProy & """"
                FuncionAbrirVentana(url)
            End If
        Catch ex As Exception
            Me.Mensaje(ex.ToString)
        End Try

    End Sub

    Private Sub FuncionAbrirVentana(ByVal url As String)
        ClientScript.RegisterStartupScript(Me.GetType(), "AbrirVentana;", "window.open(" & url & ",'','width=940,height=700,menubar=0,resizable=0,scrollbars=1');", True)
    End Sub

    Protected Sub Btn_Ingresar_Planificacion_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Btn_Ingresar_Planificacion.Click

        Dim plani As Planificacion = New Planificacion(CInt(Me.DdlOT.Text), System.DateTime.Now, , Me.TxtDensidad.Text)
        Try
            CL.ingresarPlanificacionPrincipal(plani)
            dtDatosPlanificacion.Columns.Add(New DataColumn("NPLANIFICACION"))
            dtDatosPlanificacion.Columns.Add(New DataColumn("FECHA"))
            Me.llenarGridPlanificacionPrincipal()
            Me.LblMsje.Text = ("Se ha Creado Una Nueva Planificacion Para la OT N° " & Me.DdlOT.Text)
            Me.LblMsje.ForeColor = Drawing.Color.Blue
            Me.LblMsje.Font.Bold = True
        Catch ex As Exception
            Me.Mensaje("Error al ingresar a la BD" + ex.ToString)
            Me.LblMsje.ForeColor = Drawing.Color.Red
            Me.LblMsje.Font.Bold = True
        End Try

    End Sub

    Protected Sub DdlOT_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DdlOT.SelectedIndexChanged
        dtDatosPlanificacion.Columns.Add(New DataColumn("NPLANIFICACION"))
        dtDatosPlanificacion.Columns.Add(New DataColumn("FECHA"))
        Me.llenarGridPlanificacionPrincipal()
    End Sub
End Class
